package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"sync"
	"time"
)

var (
	n      = 10
	result []int
)

type worker struct {
	in  chan int
	out chan int
}

func doWork(id int) worker {
	w := worker{
		in:  make(chan int),
		out: make(chan int),
	}
	go func() {
		for n := range w.in {
			time.Sleep(time.Duration(rand.Intn(1000)))
			w.out <- n
		}
	}()
	return w
}

func work() {
	var workers [10]worker
	for i := 0; i < n; i++ {
		workers[i] = doWork(i)
	}

	for i, w := range workers {
		w.in <- 'a' + i
	}

	for _, w := range workers {
		result = append(result, <-w.out)
	}

	for _, v := range result {
		fmt.Printf("%c", v)
	}
}

func second() {
	wg := sync.WaitGroup{}
	in := make(chan int)
	out := make(chan int)

	// todo ???
	go func() {
		for n := range out {
			fmt.Printf("%c", n)
		}
	}()

	for i := 0; i < n; i++ {
		go func() {
			for n := range in {
				out <- n
				wg.Done()
			}
		}()
	}

	for i := 0; i < n; i++ {
		wg.Add(1)
		in <- 'a' + i
	}
	wg.Wait()
}

func AHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "B serviece")
}

func main() {
	http.HandleFunc("/", AHandler)
	http.ListenAndServe("0.0.0.0:8081", nil)
}
